
<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column show-overflow-tooltip align="center" v-for="item in columns" :key="item.prop" :prop="item.prop" :label="item.label">
              <template slot-scope="scope">
                <template v-if="item.type === 'select'">
                  <el-tag :type="item.tagType" v-if="item.isTag">{{ item.mapping[scope.row[item.prop]] }}</el-tag>
                  <span v-else>{{ item.mapping[scope.row[item.prop]] }}</span>
                </template>
                <span v-else>{{ scope.row[item.prop] }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="220px">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
                <el-dropdown trigger="click" v-if="scope.row.audit === -1" @command="(val) => onAudit(scope.row, val)">
                  <el-button type="text" style="margin:0 10px">审核</el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item :command="1">通过</el-dropdown-item>
                    <el-dropdown-item :command="0">拒绝</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <el-button type="text" style="color: #F82222" @click="onResetPassword(scope.row)">重置密码</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="page-box">
          <el-pagination
              background
              :current-page="pageNo"
              :page-sizes="pageSizes"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalSize"
              @size-change="changePageSize"
              @current-change="changePageNo"
          />
        </div>
      </el-card>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
export default {
  name: "index",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.user,
      defaultParams: {
        role: 2
      },
      searchList: [
        {
          label: '审核状态',
          prop: 'audit',
          name: 'select',
          clearable: true,
          options: [
            { label: '全部', value: '' },
            { label: '待审核', value: -1 },
            { label: '审核通过', value: 1 },
            { label: '审核不通过', value: 0 }
          ]
        },
        {
          label: '公司名称',
          prop: 'company_name',
          name: 'input',
          clearable: true
        },
        {
          label: '姓名',
          prop: 'nickname',
          name: 'input',
          clearable: true
        },
        {
          label: '手机号',
          prop: 'phone',
          name: 'input',
          clearable: true
        },
      ],
      searchParams: {
        audit: '',
        company_name: '',
        nickname: '',
        phone: ''
      },
      columns: [
        { prop: 'company_name', label: '公司名称' },
        { prop: 'nickname', label: '姓名' },
        { prop: 'phone', label: '手机号' },
        { prop: 'address', label: '联系地址' },
        { prop: 'balance', label: '钱包余额' },
        { prop: 'status', label: '启用状态', type: 'select', mapping: { 0: '禁用', 1: '启用'} },
        { prop: 'audit', label: '审核状态', type: 'select', mapping: { '-1': '待审核', 0: '不通过', 1: '已通过'} },
        { prop: 'audit_desc', label: '审核备注' },
        { prop: 'create_time', label: '创建时间' },
      ],
      tableData: [

      ]
    }
  },
  methods: {
    onResetPassword(row) {
      http.user.update(row.id, { password: '123456' }).then(() => {
        this.$message.success('重置密码成功')
      })
    },
    onAudit(row, audit) {
      console.log(audit)
      // 对话框提示输入审核原因
      this.$prompt('请输入审核原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        // 必填校验
        inputValidator: (value) => {
          if (!value) {
            return '请输入审核原因';
          }
        }
      }).then(({ value }) => {
        http.user.update(row.id, { audit, audit_desc: value }).then(() => {
          this.$message.success('操作成功')
          this.initData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });
      });
    }
  }
}
</script>

<style scoped lang="scss">

</style>
